﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using LockerRack.Drawers;
using LockerRack.Interfaces;
using Primitive.Mvvm.Framework.Interfaces;
using UIController.ViewModels;

namespace UIController.Views
{
    public partial class RightDetailView : UserControl, IWindowContainer
    {
        private RightDetailViewModel _model;
        
        public RightDetailView()
        {
            InitializeComponent();
        }

        public bool Initialize(IModel model, IMediator mediator, List<Exception> exceptions)
        {
            _model = new RightDetailViewModel(model, mediator);
            _model.RefreshViewFunc = OnRefresh;
            return Refresh(_model.SelectedDrawer, exceptions);
        }

        private bool OnRefresh(IViewCallback obj)
        {
            if (!_model.CanRefreshView(obj))
                return false;

            var listExc = new List<Exception>();
            var res = Refresh(_model.SelectedDrawer, listExc);
            if(listExc.Count > 0)
            {
                var ecx = new AggregateException("Refresh exception in RightDetailView", listExc);
                throw ecx;
            }

            return res;
        }

        private bool Refresh(Drawer drawer, List<Exception> exceptions)
        {
            ClearFlowPanel();

            return RecurciveRefresh(drawer, exceptions);
        }

        private bool RecurciveRefresh(Drawer drawer, List<Exception> exceptions)
        {
            if (drawer == null)
                return false;

            if (drawer.Files == null)
                return false;

            if (exceptions == null)
                exceptions = new List<Exception>();

            foreach (IDrawerFile file in drawer.Files)
            {
                var taskViewModel = new TaskViewModel(_model.Model, file, _model.Mediator);
                var taskView = new TaskView();
                if (taskView.Initialize(taskViewModel, _model.Mediator, exceptions))
                    flowLayoutPanel.Controls.Add(taskView);
            }

            if (drawer.Drawers != null)
            {
                foreach (Drawer d in drawer.Drawers)
                {
                    RecurciveRefresh(d, exceptions);
                }
            }
            return true;
        }

        private void ClearFlowPanel()
        {
            var controls = flowLayoutPanel.Controls;
            flowLayoutPanel.Controls.Clear();
            foreach (Control control in controls)
            {
                control.Dispose();
            }
        }

        public void Clear(out Exception exception)
        {
            exception = null;
            try
            {
                foreach (var control in flowLayoutPanel.Controls)
                {
                    var taskView = control as TaskView;
                    if(taskView != null)
                    {
                        taskView.Clear(out exception);
                    }
                }
                flowLayoutPanel.Controls.Clear();
                _model = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }
    }
}
